package com.feng.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.feng.commonutils.R;
import com.feng.eduservice.client.VodClient;
import com.feng.eduservice.entity.EduVideo;
import com.feng.eduservice.mapper.EduVideoMapper;
import com.feng.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feng.servicebase.excecption.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author fengge
 * @since 2023-01-17
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {
    @Autowired
    private VodClient vodClient;
    @Override
    public R getVideoInfo(String id) {
        EduVideo list = this.getById(id);
        return R.ok().data("video",list);
    }

    @Override
    @Transactional
    public R removeVideoById(String id) {
        EduVideo video = this.getById(id);

        //根据视频id删除阿里上的视频
        String videoSourceId = video.getVideoSourceId();
        //判断小节是否有视频id
        if(!StringUtils.isEmpty(videoSourceId)){
            R result = vodClient.deleteAkVideo(videoSourceId);
            if(result.getCode()==20001){
                throw new GuliException(20001,"删除视频失败!");
            }
        }
        //删除小节的详情信息
        this.removeById(id);


         return R.ok();
    }
}
